#include "SuperStack.h"
void SuperStack::locateStackPointer(){
    if(sp!=stack.end()-1)
	{
        stack.erase(sp+1,stack.end()-1);
	}
}
// add/delete record
void SuperStack::addLog(Operation op,Record r, int option)
{
	//reset flag
	canUndo=true;

    if (stack.size()!=0)
	{
		locateStackPointer();
	}

    log.set(op,r,option);
    stack.push_back(log);
    sp=stack.end()-1;
}
// modify record
void SuperStack::addLog(Operation op,Record oldRecord,Record newRecord,int option)
{
	//reset flag
    canUndo=true;

    if (stack.size()!=0)
	{
		locateStackPointer();
	}

    log.set(op,oldRecord,newRecord,option);
    stack.push_back(log);
    sp=stack.end()-1;
}
bool SuperStack::can(Operation op)
{
    if(stack.size()==0)
        return false;

    if(op==undo_Record)
	{
        if(sp==stack.begin())
		{
			if(canUndo)
			{
                thisLog=*sp;
                canUndo=false;
				return true;
			}
			else
			{
				return false;
			}
		}
		else
		{
            canUndo=true;
            thisLog=*sp;
			sp--;
			return true;
		}
	}
}
OperationLog SuperStack::getLog(){
    return thisLog;
}
void SuperStack::print()
{
     qDebug()<<"the logs in stack: "<<endl;
    for (unsigned int i=0;i<stack.size();i++)
	{
         qDebug()<<"log("<<i<<") :"<<endl;
        stack.at(i).print();
    }
     qDebug()<<"Stack pointer points to thisLog :"<<endl;
    sp->print();
     qDebug()<<"Still can preform Undo?	";
     if(can(undo_Record))
         qDebug()<<"YES"<<endl;
    else if(!can(undo_Record))
         qDebug()<<"NO"<<endl;
	else
         qDebug()<<"Unknown"<<endl;
}
